/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * Superclass for clipboard related handlers providing safe creation and disposal of the {@link Clipboard}.
 */
public abstract class ClipboardHandler extends AbstractHandler {
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		final Clipboard clipboard = new Clipboard(HandlerUtil.getActiveShell(event).getDisplay());
		try {
			return execute(event, clipboard);
		} finally {
			clipboard.dispose();
		}
	}

	protected abstract Object execute(ExecutionEvent event, Clipboard clipboard) throws ExecutionException;
}
